<?php
/**
 * Created by PhpStorm.
 * User: Dmitry
 * Date: 28.02.2015
 * Time: 4:38
 */

include_once "Context.php";
$method = $_POST['method'];

function GetCategories(){
    return Context::Admin()->GetCategories();
}

function GetProducts(){
    return Context::Admin()->GetProducts();
}

function DeleteCategory($data){
        $CategoryId = $data["Id"];
        $result = Context::DB()->query("DELETE FROM Categories WHERE Id = '$CategoryId'");
        return "{result : true}";
}

function SaveCategory($data)
{
    $id = $data["Id"];
    $Title = $data["Title"];

    Context::DB()->prepareQuery("UPDATE Categories SET Title = '$Title' WHERE  Id = $id");
    return "{result : true}";
}

function AddCategoryData($data)
{
    $categoryTitle = $data["Title"];
    $result = Context::DB()->prepareQuery("INSERT INTO `Categories`(`Title`) VALUES ('$categoryTitle')");
    return $result;
}

function AddProduct($data)
{
    $Title = $data["Title"];
    $Download_link = $data["Donwload_link"];
    $image = $data["Image"];
    $Image_name = $data["Image_name"];
    $File_size =  $data["File_size"];
    $Requirements = $data["Requirements"];
    $Languages = $data["Languages"];
    $Date = $data["Date"];
    $Author = $data["Author"];
    $Description_preview = $data["Description_preview"];
    $Description = $data["Description"];
    $Category_Name =  $data["Category_Name"];
    $Home_page = $data["Home"];
    $path = "../images/$Image_name";
    $result = base64_to_jpeg($image,$path);
    $CurrentCategory = Context::DB()->queryAssoc("SELECT Id FROM Categories WHERE Title = '$Category_Name'");
    $CategoryId = $CurrentCategory[0]["Id"];
    $result = Context::DB()->prepareQuery("INSERT INTO `Products`(`TItle`,`Download_link`, `Image`, `File_size`, `Requirements`, `Languages`, `Date_added`, `Author`, `Homepage`, `Description_preview`, `Description`, `CategoryId`) VALUES ('$Title','$Download_link','$Image_name','$File_size','$Requirements','$Languages','$Date','$Author','$Home_page','$Description_preview','$Description','$CategoryId')");

}
function AddUser($data){
    $Login = $data["Login"];
    $Password = $data["Password"];
    $Role = $data["Role"];
    $CurrentRole = Context::DB()->queryAssoc("SELECT Id FROM Role WHERE Title = '$Role'");
    $result = Context::DB()->prepareQuery("INSERT INTO `Admin`(`Login`,`Password`,`RoleId`) VALUES ('$Login','$Password',$RoleId)");


}

function base64_to_jpeg($base64_string, $output_file) {
    $ifp = fopen($output_file, "wb");

    $data = explode(',', $base64_string);

    fwrite($ifp, base64_decode($data[1]));
    fclose($ifp);

    return $output_file;
}

function DeleteProduct($data)
{
    $ProductId = $data["Id"];
    $result = Context::DB()->query("DELETE FROM Products WHERE Id = '$ProductId'");
    return "{result : true}";
}

function SaveProduct($data){
    $ProductId = $data['ProductId'];
    $Title = $data["Title"];
    $image = $data["Image"];
    $File_size =  $data["File_size"];
    $Requirements = $data["Requirements"];
    $Languages = $data["Languages"];
    $Date = $data["Date_added"];
    $Author = $data["Author"];
    $Description_preview = $data["Description_preview"];
    $Description = $data["Description"];
    $CategoryId =  $data["CategoryId"];
    $Home_page = $data["Homepage"];
    $result = Context::DB()->prepareQuery("UPDATE `Products` SET `TItle`= '$Title',`Image`= '$image',`File_size`='$File_size',`Requirements`='$Requirements',`Languages`= '$Languages',`Date_added`='$Date',`Author`='$Author',`Homepage`='$Home_page',`Description_preview`='$Description_preview',`Description`='$Description',`CategoryId`='$CategoryId' WHERE Id = '$ProductId'");
}

function UpdateProduct($data){
    $ProductId = $data['Id'];
    $Title = $data["Title"];
    $Download_link = $data["Download_link"];
    $image = $data["Image"];
    $File_size =  $data["File_size"];
    $Requirements = $data["Requirements"];
    $Languages = $data["Languages"];
    $Date = $data["Date_added"];
    $Author = $data["Author"];
    $Description_preview = $data["Description_preview"];
    $Description = $data["Description"];
    $CategoryId =  $data["CategoryId"];
    $Home_page = $data["Homepage"];
    $result = Context::DB()->prepareQuery("UPDATE `Products` SET `Title`= '$Title',`Download_link`= '$Download_link',`Image`= '$image',`File_size`='$File_size',`Requirements`='$Requirements',`Languages`= '$Languages',`Date_added`='$Date',`Author`='$Author',`Homepage`='$Home_page',`Description_preview`='$Description_preview',`Description`='$Description',`CategoryId`='$CategoryId' WHERE Id = '$ProductId'");
    return $result;
}

function getOtherInfo(){
    return Context::Admin()->GetOtherInfo();
}

function SaveTelephone($data)
{
    $id = $data["Id"];
    $tel = $data["Tel"];
    Context::DB()->prepareQuery("UPDATE OtherInfo SET Telephone = '$tel' WHERE  Id = $id");
    return "{result : true}";
}
function SaveAdv($data){
    $id = $data["Id"];
    $code = $data["Code"];
    Context::DB()->prepareQuery("UPDATE Advert SET Code = '$code' WHERE  Id = $id");
    return "{result : true}";
}

function AddNews($data){
    $Title = $data["Title"];
    $Date = $data["Date"];
    $Description = $data["Description"];
    $result = Context::DB()->prepareQuery("INSERT INTO `News`(`Title`, `Date`, `Description`) VALUES ('$Title','$Date','$Description')");
    return $result;
}

function GetNews(){
    return Context::Admin()->GetNews();
}

function SaveNews($data){
    $id = $data["Id"];
    $Title = $data["Title"];
    $Date = $data["Date"];
    $Description = $data["Description"];
    Context::DB()->prepareQuery("UPDATE `News` SET `Title`= '$Title',`Date`='$Date',`Description`='$Description' WHERE Id = '$id'");
    return "{result : true}";
}

function DeleteNews($data)
{
    $Id = $data["Id"];
    $result = Context::DB()->query("DELETE FROM News WHERE Id = '$Id'");
    return "{result : true}";
}

function AddContactInfo($data){
    $UserName = $data['UserName'];
    $Email = $data['Email'];
    $Company = $data['Company'];
    $Msg = $data['Msg'];
    $result = Context::DB()->prepareQuery("INSERT INTO `ContactInfo`(`Name`, `Email`, `CompanyName`,`Subject`) VALUES ('$UserName','$Email','$Company','$Msg')");
    echo "true";

}

function GetContactInfo(){
    return Context::Admin()->GetContactInfo();
}

function DeleteMsg($data){
    $Id = $data["Id"];
    $result = Context::DB()->query("DELETE FROM ContactInfo WHERE Id = '$Id'");
    return "{result : true}";
}

function addToSlider($productId)
{
    return Context::Admin()->addToSlider($productId);
}

function getSlides()
{
    return Context::Admin()->getSliderSlides();
}

function removeSlide($id)
{
    return Context::Admin()->removeSlide($id);
}

function GetCurrentProduct($data){
    $id = $data["Id"];
    return Context::Admin()->GetCurrentProduct($id);
}

function getNewProducts(){
    return Context::Admin()->GetNewProduct();
}
function GetProductByCategory($data){
    $id = $data["Id"];
    return Context::Admin()->GetProductByCategory($id);
}
function GetUsers(){
    return Context::Admin()->GetUsers();
}
function getRoles(){
    return Context::Admin()->GetRoles();
}
function DeleteUser($data){
    $Id = $data["Id"];
    $result = Context::DB()->query("DELETE FROM Admin WHERE Id = '$Id'");
    return "{result : true}";
}
function SaveUser($data){
    $login = $data["Login"];
    $Password = $data["Password"];
    $id = $data["Id"];
    Context::DB()->prepareQuery("UPDATE `Admin` SET `Login`= '$login',`Password`='$Password' WHERE Id = '$id'");
}
function AddCountDownload($data)
{
    $id = $data["Id"];
    $down = (int)$data["Download"];
    $down++;
    $result = Context::DB()->prepareQuery("UPDATE `Products` SET `Download` ='$down' WHERE Id = '$id'");
    echo $result;

}
function GetAdvrt(){
    return Context::Admin()->GetAdv();
}
function GetLinks(){
    return Context::Admin()->GetLinks();
}
function SaveLink($data){
    $Link = $data["Link"];
    $Time = $data["Time"];
    $id = $data["Id"];
    Context::DB()->prepareQuery("UPDATE `Auto` SET `Link`= '$Link',`Time`='$Time' WHERE Id = '$id'");
}


switch($method)
{
    case 'SaveLink':
        return SaveLink($_POST['data']);
    case 'GetLinks':
        return GetLinks();
    case 'SaveAdv':
        return SaveAdv($_POST['data']);
    case 'GetAdvrt':
        return GetAdvrt();
    case 'AddCountDownload':
        return AddCountDownload($_POST['data']);
    case 'SaveUser':
        return SaveUser($_POST['data']);
    case 'DeleteUser':
        return DeleteUser($_POST['data']);
    case 'AddUser':
        return AddUser($_POST['data']);
    case 'getRoles':
        return getRoles();
    case 'getUsers':
        return GetUsers();
    case 'GetProductByCategory':
        return GetProductByCategory($_POST['data']);
    case 'getNewProducts':
        return getNewProducts();
    case 'AddCategory':
        return AddCategoryData($_POST['data']);
    case 'getCategories':
        return getCategories();
    case 'DeleteCategory':
        return DeleteCategory($_POST['data']);
    case 'SaveCategory':
        return SaveCategory($_POST['data']);
    case 'AddProduct' :
        return AddProduct($_POST['data']);
    case 'getProducts':
        return GetProducts();
    case 'DeleteProduct':
        return DeleteProduct($_POST['data']);
    case 'SaveProduct':
        return SaveProduct($_POST['data']);
    case 'UpdateProduct':
        return UpdateProduct($_POST['data']);
    case 'getOtherInfo':
        return getOtherInfo();
    case 'SaveTelephone':
        return SaveTelephone($_POST['data']);
    case 'AddNews':
        return AddNews($_POST['data']);
    case 'GetNews':
        return GetNews();
    case 'SaveNews':
        return SaveNews($_POST['data']);
    case 'DeleteNews':
        return DeleteNews($_POST['data']);
    case 'AddContactInfo':
        return AddContactInfo($_POST['data']);
    case 'GetContactInfo':
        return GetContactInfo();
    case 'DeleteMsg':
        return DeleteMsg($_POST['data']);
    case 'addToSlider':
        return addToSlider($_POST['productId']);
    case 'getSlides':
        return getSlides();
    case 'removeSlide':
        return removeSlide($_POST['slideId']);
    case 'GetCurrentProduct':
        return GetCurrentProduct($_POST['data']);
    case 'DownLoadFile':
        return DownloadFile($_POST['data']);
}
